{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c6409153-313e-4911-b999-fce3fa5b3e0d",
   "metadata": {},
   "source": [
    "# Comparators\n",
    "\n",
    "The following comparators are supported:\n",
    "\n",
    "- Equal (denoted as '==')\n",
    "- NotEqual (denoted as '!=')\n",
    "- GreaterThan (denoted as '>')\n",
    "- GreaterEqual (denoted as '>=')\n",
    "- LessThan (denoted as '<')\n",
    "- LessEqual (denoted as '<=')\n",
    "\n",
    "Note that integer and fixed-point numbers are represented in a 2-complement method during function evaluation.\n",
    "The binary number is extended in the case of a register size miss-match.\n",
    "For example, the positive signed number $(110)_2=6$ is expressed as $(00110)_2$ when operating with a 5-qubit register.\n",
    "Similarly, the negative signed number $(110)_2=-2$ is expressed as $(11110)_2$.\n",
    "\n",
    "Examples:\n",
    "\n",
    "(5 <= 3) = 0\n",
    "\n",
    "(5 == 5) = 1\n",
    "\n",
    "($(011)_2$ == $(11)_2$) = 1\n",
    "\n",
    "(signed $(101)_2$ < unsigned $(101)_2$) = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52f4a3fd-ed76-4cb8-9145-584e82163533",
   "metadata": {},
   "source": [
    "## Examples\n",
    "\n",
    "#### Example 1: Comparing Two Quantum Variables\n",
    "\n",
    "This example generates a quantum program that performs 'equal' between two variables.\n",
    "The left arg is a signed variable with 5 qubits and the right arg is an unsigned varialbe with 3 qubits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c57c4fc6-98ab-4d49-9029-7b48411b25d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:35.772459Z",
     "iopub.status.busy": "2024-05-07T13:23:35.771993Z",
     "iopub.status.idle": "2024-05-07T13:23:38.883199Z",
     "shell.execute_reply": "2024-05-07T13:23:38.882528Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, QNum, allocate_num, create_model, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], b: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate_num(5, True, 0, a)\n",
    "    allocate_num(3, False, 0, b)\n",
    "\n",
    "    res |= a == b\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4c8aebb5-1e29-4062-a95d-6068057ecc8a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:38.886550Z",
     "iopub.status.busy": "2024-05-07T13:23:38.885738Z",
     "iopub.status.idle": "2024-05-07T13:23:41.192905Z",
     "shell.execute_reply": "2024-05-07T13:23:41.192140Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"comparator_2vars_example\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ad9c0c6-857d-4e15-8735-c4008bafe537",
   "metadata": {},
   "source": [
    "#### Example 2: Comparing Integer and Quantum Variable\n",
    "\n",
    "This example generates a quantum program that performs 'less equal' between a quantum register and an integer. \n",
    "The left arg is an unsigned quantum variable with 3 qubits, and the right arg is an integer equal to 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9ea8e6c8-6fc7-418a-91b1-811dbd4fd996",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:41.198285Z",
     "iopub.status.busy": "2024-05-07T13:23:41.196911Z",
     "iopub.status.idle": "2024-05-07T13:23:41.208148Z",
     "shell.execute_reply": "2024-05-07T13:23:41.207522Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    QNum,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    hadamard_transform,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(a: Output[QNum], res: Output[QNum]) -> None:\n",
    "    allocate(3, a)\n",
    "    hadamard_transform(a)\n",
    "    res |= a <= 2\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0f389b0c-2388-4dd0-b79c-4f82c36b5d89",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:23:41.212470Z",
     "iopub.status.busy": "2024-05-07T13:23:41.211253Z",
     "iopub.status.idle": "2024-05-07T13:23:44.835166Z",
     "shell.execute_reply": "2024-05-07T13:23:44.833836Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 4.0, 'res': 0.0}: 150,\n",
       " {'a': 1.0, 'res': 1.0}: 138,\n",
       " {'a': 7.0, 'res': 0.0}: 132,\n",
       " {'a': 2.0, 'res': 1.0}: 126,\n",
       " {'a': 6.0, 'res': 0.0}: 123,\n",
       " {'a': 3.0, 'res': 0.0}: 115,\n",
       " {'a': 5.0, 'res': 0.0}: 110,\n",
       " {'a': 0.0, 'res': 1.0}: 106]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from classiq import execute, synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"comparator_integer_example\")\n",
    "qprog = synthesize(qmod)\n",
    "\n",
    "result = execute(qprog).result()[0].value\n",
    "result.parsed_counts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
